package com.gdth.bank.activity;

import java.util.ArrayList;

import com.gdth.bank.R;
import com.gdth.bank.adapter.PhotoPageAdapter;
import com.gdth.bank.widget.image.Bimp;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;
import com.gdth.widget.dialog.BaseToast;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class PhotoActivity extends BaseActivity{
	@ViewInject(id=R.id.viewpager) ViewPager pager;
	
	@ViewInject(id=R.id.photo_relativeLayout) RelativeLayout photo_relativeLayout;
	
	@ViewInject(id=R.id.photo_bt_exit, click="onFinishClick") Button photo_bt_exit;
	@ViewInject(id=R.id.photo_bt_del, click="onDeleteClick") Button photo_bt_del;
	@ViewInject(id=R.id.photo_bt_enter, click="onEnterClick") Button photo_bt_enter;
	
	private ArrayList<View> listViews = null;
	private PhotoPageAdapter adapter;
	private int current;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photo);

		photo_relativeLayout.setBackgroundColor(0x70000000);

		pager.addOnPageChangeListener(pageChangeListener);
		
		for (int i = 0; i < Bimp.getInstance().getCount(); i++) {
			initListViews(Bimp.getInstance().getBmp().get(i));//
		}

		adapter = new PhotoPageAdapter(listViews);//  构造adapter
		pager.setAdapter(adapter);//  设置适配器
		
		Intent intent = getIntent();
		int id = intent.getIntExtra("ID", 0);
		boolean isDelete = intent.getBooleanExtra("delete", false);
		if (!isDelete)
			photo_bt_del.setVisibility(View.GONE);
		pager.setCurrentItem(id);
	}


	private void initListViews(Bitmap bm) {
		if (listViews == null)
			listViews = new ArrayList<View>();
		ImageView img = new ImageView(this);
		img.setBackgroundColor(0xff000000);
		img.setImageBitmap(bm);
		img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		listViews.add(img);
	}
	
	public void onEnterClick(View v) {
//			FileUtils.delFile(del.get(i) + ".JPEG");
		finish();
	}
	
	public void onDeleteClick(View v) {
//		if (listViews.size() == 1) {
//			Bimp.getInstance().reset();
//			FileUtils.deleteDir();
//			finish();
//		} else {
		if (Bimp.getInstance().getDrr().get(current).getSczt() != 1) {
			pager.removeAllViews();
			listViews.remove(current);
			Bimp.getInstance().getBmp().remove(current);
			Bimp.getInstance().getDrr().remove(current);
			Bimp.getInstance().setMax(Bimp.getInstance().getMax() - 1);
			if(listViews.size()==0){
				finish();
				
			} else {
				adapter.setListViews(listViews);
				adapter.notifyDataSetChanged();
			}
		} else {
			BaseToast.getInstance().ShowSystemToast(this, getResources().getString(R.string.param254));
		}
//		}
	}
	
	public void onFinishClick(View v){
		finish();
	}

	private OnPageChangeListener pageChangeListener = new OnPageChangeListener(){
		public void onPageSelected(int arg0) {
			current = arg0;
		}

		public void onPageScrolled(int arg0, float arg1, int arg2){

		}

		public void onPageScrollStateChanged(int arg0){

		}
	};
}
